package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@Slf4j
@RequestMapping("admin/category")
@Api(tags = "分类管理")
@RequiredArgsConstructor
public class CategoryController {
    private final CategoryService categoryService;

    /**
     * 新增菜品分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    private Result save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success("新增分类成功");
    }

    /**
     * 分类分页查询
     *
     * @param pageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> categoryPage(CategoryPageQueryDTO pageQueryDTO) {
        log.info("分类分页查询：{}", pageQueryDTO);
        PageResult pageResult = categoryService.categoryPage(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除分类")
    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "Long")
    public Result delete(@RequestParam("id") Long id) {
        log.info("删除分类:{}", id);
        categoryService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 更新分类
     *
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "更新分类")
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        log.info("更新分类:{}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success("更新成功");
    }

    /**
     * 菜品分类的启用和禁用
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation(value = "菜品分类的启用和禁用")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "Integer"),
                    @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "Long")}
    )
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("更新分类状态:{},{}", status, id);
        categoryService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    @ApiImplicitParam(name = "type", value = "分类类型", required = true, dataType = "Integer")
    public Result<List<Category>> list(Integer type) {
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}
